﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using XnaXaml.Controls;

namespace XnaXaml.Xaml
{
    public class XamlDocument
    {
        public List<XNamespace> Namespaces { get; set; } 
        public XDocument XDoc { get; set; }

        public XNamespace Namespace
        {
            get
            {
                return Namespaces.Count > 0 ? Namespaces[0] : "";
            }
        }

        public XamlDocument() { }
        public XamlDocument(string xmlContent)
        {
            Namespaces = new List<XNamespace>();
            Namespaces.Add("http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            XDoc = XDocument.Parse(xmlContent);
        }

        public void Load(string xmlContent)
        {
            XDoc = XDocument.Parse(xmlContent);
            var ns = Namespaces.Count > 0 ? Namespaces[0] : "";
            XElement root = XDoc.Element(ns + "Page");
            Debug.WriteLine(root.Name);

            var elements = (from element in root.Elements() select element).ToList();
            foreach (var element in elements)
            {
                Debug.WriteLine(element);
            }
        }

        public Page ToXna()
        {
            XElement root = XDoc.Element(Namespace + "Page");
            if (root == null) return null;
            return new Page(root);
        }

        
    }
}
